package com.drondea.testclient.util;

import lombok.extern.slf4j.Slf4j;
import org.yaml.snakeyaml.Yaml;

import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.ByteBuffer;
import java.util.Map;

/**
 * YAML工具类
 *
 * @author shd
 */

@Slf4j
public class YamlUtil {

    /**
     * 保存信息
     *
     * @param msg
     * @param file
     * @throws Exception
     */
    public static void save(Map msg, File file) throws Exception {
        Yaml yaml = new Yaml();
        try {
            FileWriter fileWriter = new FileWriter(file);
            fileWriter.write(yaml.dump(msg));
            fileWriter.flush();
            fileWriter.close();
        } catch (Exception e) {
            log.error("yaml文件保存失败", e);
            throw new Exception("yaml文件保存失败", e);
        }
    }

    /**
     * 加载配置文件,转换为EdbConfig对象
     *
     * @param file
     * @param <T>
     * @return
     * @throws FileNotFoundException
     */
    public static <T> Map<String, T> loadConfig(File file) throws FileNotFoundException {
        Yaml yaml = new Yaml();
        InputStream inputStream = new FileInputStream(file);
        return yaml.loadAs(inputStream, Map.class);
    }


    /**
     * 将byte转为16进制
     *
     * @param bytes
     * @return
     */
    private static String byte2Hex(byte[] bytes) {
        StringBuffer stringBuffer = new StringBuffer();
        String temp = null;
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(bytes[i]);
            temp = Integer.toHexString(bytes[i] & 0xFF);
            if (temp.length() == 1) {
                // 得到一位的进行补0操作
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }

    private static byte[] hex2Byte(String hex) {
        int l = hex.length();
        byte[] data = new byte[l / 2];
        for (int i = 0; i < l; i += 2) {
            data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
                    + Character.digit(hex.charAt(i + 1), 16));
        }
        return data;
    }

    public static void main(String[] args) {
        byte[] bytes = hex2Byte("aa3c430046d0000b");
        long result = ByteBuffer.wrap(bytes).getLong();

        int month = (int) (result >>> 60 & 15L);
        int day = (int) (result >>> 55 & 31L);
        int hour = (int) (result >>> 50 & 31L);
        int minutes = (int) (result >>> 44 & 63L);
        int seconds = (int) (result >>> 38 & 63L);
        int gateId = (int) (result >>> 16 & 4194303L);
        int sequenceId = (int) (result & 65535L);
        String format = String.format("%1$02d%2$02d%3$02d%4$02d%5$02d%6$07d%7$05d", month, day, hour, minutes, seconds, gateId, sequenceId);
        System.out.println(format);


        double exportProgress = BigDecimal.valueOf(54876).multiply(BigDecimal.valueOf(100L)).
                divide(BigDecimal.valueOf(4000000), 2, RoundingMode.HALF_DOWN).doubleValue();
        System.out.println("exportProgress = " + exportProgress);
    }
}
